/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package formula.one.urjc.piloto.view;

import formula.one.urjc.Metodos;
import formula.one.urjc.piloto.Piloto;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 *
 * @author santiago
 */
public class BajaPiloto extends javax.swing.JInternalFrame {
    
    //VARIABLES GLOBALES
    //Declaramos este objeto para ejecutar los metodos genericos
    Metodos metodo = new Metodos() ;
    String nombreFichero = "pilotos.dat" ; 

    /**
     * Creates new form BajaPiloto
     */
    public BajaPiloto() {
        initComponents();
        cargarPilotos() ;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        pilotos = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        borrarPiloto = new javax.swing.JButton();
        cancelarBaja = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Baja Piloto");

        jLabel2.setText("PIloto:");

        borrarPiloto.setText("Borrar");
        borrarPiloto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                borrarPilotoActionPerformed(evt);
            }
        });

        cancelarBaja.setText("Cancelar");
        cancelarBaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarBajaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(90, 90, 90)
                        .addComponent(jLabel2)
                        .addGap(4, 4, 4)
                        .addComponent(pilotos, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(85, 85, 85)
                        .addComponent(borrarPiloto)
                        .addGap(87, 87, 87)
                        .addComponent(cancelarBaja))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(jLabel1)))
                .addContainerGap(85, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pilotos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(51, 51, 51)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(borrarPiloto)
                    .addComponent(cancelarBaja))
                .addContainerGap(146, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelarBajaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarBajaActionPerformed
        // TODO add your handling code here:
        this.dispose() ;
    }//GEN-LAST:event_cancelarBajaActionPerformed

    private void borrarPilotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_borrarPilotoActionPerformed
        // TODO add your handling code here:        
        ArrayList<Object> arrayPilotos = metodo.leerFichero(nombreFichero) ;
        int longitudArray = arrayPilotos.size() ;   
        String buscarPiloto = (String) pilotos.getSelectedItem() ;
        System.out.println("Longitud de Array Modificación: " + longitudArray) ;
        boolean encontrado = false ;
        int j = 0 ;
        while (j <= longitudArray && !encontrado ) {
            Piloto pilotoLista = (Piloto) arrayPilotos.get(j) ;
            String nombre_Piloto = pilotoLista.getNombre() ;
            String apellidos_Piloto = pilotoLista.getApellidos() ;
            String nombre_Lista = nombre_Piloto + " " + apellidos_Piloto ;
            encontrado = nombre_Lista.equals(buscarPiloto) ;
            if (encontrado) {
                System.out.println("Encontrado Borrado") ;
                System.out.println("Antes de array: " + arrayPilotos.size()) ;
                arrayPilotos.remove(j) ;
                System.out.println("Despues de array: " + arrayPilotos.size()) ;
                metodo.borrarFichero(nombreFichero) ;
                metodo.escribirFichero(nombreFichero, arrayPilotos) ;
            }
            j++ ;
        }
        pilotos.removeAllItems() ;
        cargarPilotos() ;
    }//GEN-LAST:event_borrarPilotoActionPerformed

    public void cargarPilotos() {     
        System.out.println("REGISTROS EN EL FICHERO CARGAR COMBOS") ;
        pilotos.addItem("");
        ArrayList<String> listadoPilotos = metodo.listadoPilotos() ;
        for (int j=0; j<listadoPilotos.size(); j++) {
            pilotos.addItem(listadoPilotos.get(j)) ;
        }
    }    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BajaPiloto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BajaPiloto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BajaPiloto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BajaPiloto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BajaPiloto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton borrarPiloto;
    private javax.swing.JButton cancelarBaja;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JComboBox pilotos;
    // End of variables declaration//GEN-END:variables
}
